package com.kcl.dfss.fragments;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import com.kcl.dfss.HistoryActivity;
import com.kcl.dfss.LocationService;
import com.kcl.dfss.R;
import com.kcl.dfss.utils.ApplicationData;
import com.kcl.dfss.utils.UsageAdapter;
import com.kcl.dfss.utils.UsageRecord;
import com.kcl.dfss.view.CarStateView;
import com.kcl.dfss.view.SpeedPanelView;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.Fragment;

public class StaticsFragment extends Fragment {
	private String TAG = "StaticsFragment";
	private SpeedPanelView mSpeedView = null;
	private CarStateView mDrivingStatus = null;
	private ImageView mHistory = null;
	private ListView mRecordList = null;
	private int mSpeed = 0;
	private int mOdometer = 0;
	private UsageAdapter mRecordsAdapter = null;
	private boolean StartUpdate = false;
	private LocationService mLocationService = null;
	private static final int UPDATE_SPEEDVIEW = 0;
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_SPEEDVIEW:
				updateSpeedView(mSpeed, mOdometer);
				break;
			}
		}
	};
	private Thread mUpdateSpeed = new Thread() {
		public void run() {
			while (StartUpdate) {
				Location location = LocationService.getLatestLocation();
				if (location != null) {
					mSpeed = (int) location.getSpeed();
				}
				mOdometer = ((int) LocationService.getDistance()) / 1000;
				mHandler.sendEmptyMessage(UPDATE_SPEEDVIEW);
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ArrayList<UsageRecord> records = ApplicationData.getInstance(
				getActivity()).getUsageRecord(
				new Date(System.currentTimeMillis()));
		mRecordsAdapter = new UsageAdapter(getActivity(), records);
	}
	

	private static StaticsFragment mInstance = null;

	public static StaticsFragment getInstance() {
		if (mInstance == null) {
			mInstance = new StaticsFragment();
		}
		return mInstance;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_statics, null);
		mSpeedView = (SpeedPanelView) view.findViewById(R.id.speedpanel);
		mDrivingStatus = (CarStateView) view.findViewById(R.id.car_state);
		mDrivingStatus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG, "click status");
				mHandler.sendEmptyMessage(0);
			}
		});
		mRecordList = (ListView) view.findViewById(R.id.usagelog);
		mRecordList.setAdapter(mRecordsAdapter);
		mHistory = (ImageView) view.findViewById(R.id.image_history);
		mHistory.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(getActivity(), HistoryActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getActivity().startActivity(intent);
			}
		});
		return view;
	}

	public void updateSpeedView(final int speed, final int odometer) {
		getActivity().runOnUiThread(new Runnable() {
			public void run() {
				mSpeedView.setSpeed(speed,odometer);
			}
		});
	}

	public void setUpListView(Date date) {
		ArrayList<UsageRecord> records = ApplicationData.getInstance(
				getActivity()).getUsageRecord(date);
		mRecordsAdapter.setRecordsList(records);
		mRecordList.invalidate();

		mSpeed = 0;
		mOdometer = 0;
		mHandler.sendEmptyMessage(0);

	}

	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}

	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		if(LocationService.getInstance() != null){
			
		}
	}

	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			int year = data.getIntExtra("year", 0); // 得到年
			int month = data.getIntExtra("month", 0); // 得到月
			int day = data.getIntExtra("day", 0); // 得到天

			// 格式化日期显示
			final Calendar dat = Calendar.getInstance();
			dat.set(Calendar.YEAR, year);
			dat.set(Calendar.MONTH, month);
			dat.set(Calendar.DAY_OF_MONTH, day);

			// 显示日期结果
			// SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd");
			// Toast.makeText(TestActivity.this, format.format(dat.getTime()),
			// Toast.LENGTH_LONG).show();
		}
	}
}
